package com.example.sunmiv1.adapter;

import android.content.Context;
import android.os.Bundle;
//import androidx.annotation.NonNull;
//import android.support.v4.content.ContextCompat;

import androidx.annotation.NonNull;
import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.example.sunmiv1.R;
import com.example.sunmiv1.entity.member.MemberLabelManagerInfo;
import com.example.sunmiv1.utils.Tools;
import com.example.sunmiv1.utils.net.Common;
import com.example.sunmiv1.utils.net.HttpUtils;
import com.example.sunmiv1.utils.net.LoadingDialog;
import com.example.sunmiv1.utils.net.TextCallBack;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.List;

public class VipLabelListAdapter extends RecyclerView.Adapter<VipLabelListAdapter.ViewHolder> {

    private List<MemberLabelManagerInfo.DataBean.ListBean> listBeans;
    private Context context;
    private LoadingDialog loadingDialog;
    private String vid;
    private int size;

    public VipLabelListAdapter(String vid, Context context, List<MemberLabelManagerInfo.DataBean.ListBean> listBeans) {
        this.context = context;
        this.listBeans = listBeans;
        loadingDialog = new LoadingDialog(context);
        this.vid = vid;
    }

    public void setSize(int size) {
        this.size = size;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_vip_label_list, null);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder viewHolder, int i) {
        MemberLabelManagerInfo.DataBean.ListBean bean = listBeans.get(i);
        if (bean.isCheck()) {
            viewHolder.vipLabelListName.setBackgroundResource(R.drawable.f99_round_50);
            viewHolder.vipLabelListName.setTextColor(ContextCompat.getColor(context, R.color.f99b68));
        } else {
            viewHolder.vipLabelListName.setBackgroundResource(R.drawable.f6_round_50);
            viewHolder.vipLabelListName.setTextColor(ContextCompat.getColor(context, R.color.text_333));
        }
        viewHolder.vipLabelListName.setText(bean.getName());
        viewHolder.vipLabelListName.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (bean.isCheck()) {
                    deleteLabel(false, i);
                } else {
                    if (size == 20) {
                        Toast.makeText(context, "最多隻能設置20個標籤", Toast.LENGTH_SHORT).show();
                        return;
                    }
                    deleteLabel(true, i);
                }
            }
        });
    }


    /**
     * 刪除用戶標籤
     * operation true添加 false刪除
     */
    private void deleteLabel(boolean operation, int position) {
        loadingDialog.show();
        JSONObject jsonObject = new JSONObject();
        String url = "";
        try {
            jsonObject.put("vid", vid);
            jsonObject.put("vip_label_id", listBeans.get(position).getId());
            jsonObject = Tools.createParameterCode(jsonObject);
            if (operation) {
                url = Common.ADD_USER_LABEL;
            } else {
                url = Common.DEL_USER_LABEL;
            }
            HttpUtils.post(context, url, jsonObject, new TextCallBack() {
                @Override
                protected void onSuccess(String text) {
                    loadingDialog.dismiss();
                    try {
                        JSONObject json = new JSONObject(text);
                        Toast.makeText(context, json.getString("msg"), Toast.LENGTH_SHORT).show();
                        if (json.getInt("status") == 1) {
                            listBeans.get(position).setCheck(operation);
                            if (operation) {
                                size++;
                            } else {
                                size--;
                            }
                            notifyItemChanged(position);
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }

                @Override
                protected void onFailure(ResponseException e) {
                    loadingDialog.dismiss();
                }
            });
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    @Override
    public int getItemCount() {
        return listBeans.size();
    }

    class ViewHolder extends RecyclerView.ViewHolder {

        TextView vipLabelListName;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            vipLabelListName = itemView.findViewById(R.id.vipLabelList_name);
        }
    }

}
